package com.zhixi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhixi.common.CategoryException;
import com.zhixi.common.R;
import com.zhixi.common.SharedVariablesCurrent;
import com.zhixi.pojo.ShoppingCart;
import com.zhixi.service.ShoppingCartService;
import com.zhixi.mapper.ShoppingCartMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zhangzhixi
 * @description 针对表【shopping_cart(购物车)】的数据库操作Service实现
 * @createDate 2022-07-14 17:45:27
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart>
        implements ShoppingCartService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R<ShoppingCart> delShoppingCart(ShoppingCart shoppingCart) {
        // 1、设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = SharedVariablesCurrent.getCurrent();
        shoppingCart.setUserId(currentId);

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        // 2、要查询哪个用户的购物车信息
        queryWrapper.eq(ShoppingCart::getUserId, currentId);

        // 3、判断删除的是菜品还是套餐，哪个不为空表明用户删除的就是该菜品/套餐。
        queryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart shoppingCartOne = this.getOne(queryWrapper);
        // 防止用户删除的是不存在的菜品/套餐
        if (shoppingCartOne == null) {
            throw new CategoryException("购物车中没有该菜品/套餐");
        }

        // 4、执行删除菜品信息
        if (shoppingCartOne.getDishId() != null) {
            // 删除菜品信息-只剩一个菜品
            if (shoppingCartOne.getNumber() == 1) {
                this.remove(new LambdaQueryWrapper<ShoppingCart>()
                        .eq(ShoppingCart::getUserId, currentId)
                        .eq(ShoppingCart::getDishId, shoppingCart.getDishId()));
            }
            // 菜品数量-1，这种方案相比于下面注释的，只需要set一个字段的值即可
            this.update(new LambdaUpdateWrapper<ShoppingCart>()
                    .eq(ShoppingCart::getId, shoppingCartOne.getId())
                    .set(ShoppingCart::getNumber, shoppingCartOne.getNumber() - 1));

//           该实现方式会将所有字段再重新set一遍，效率不高
//           shoppingCartOne.setNumber(shoppingCartOne.getNumber() - 1);
//           shoppingCartService.updateById(shoppingCartOne);

            return R.success(shoppingCartOne);
        }

        // 5、执行删除套餐信息
        if (shoppingCartOne.getNumber() == 1) {
            this.remove(new LambdaQueryWrapper<ShoppingCart>()
                    .eq(ShoppingCart::getUserId, currentId)
                    .eq(ShoppingCart::getSetmealId, shoppingCartOne.getSetmealId()));
        }

        // 套餐数量-1
        /*shoppingCartOne.setNumber(shoppingCartOne.getNumber() - 1);
        shoppingCartService.updateById(shoppingCartOne);*/
        this.update(new LambdaUpdateWrapper<ShoppingCart>()
                .eq(ShoppingCart::getId, shoppingCartOne.getId())
                .set(ShoppingCart::getNumber, shoppingCartOne.getNumber() - 1));
        return R.success(shoppingCartOne);
    }
}




